package com.easyui_server.service.impl;

import com.easyui_server.dao.GroupDao;
import com.easyui_server.dao.UserDao;
import com.easyui_server.model.Group;
import com.easyui_server.model.User;
import com.easyui_server.service.GroupService;
import com.solution.framework.service.impl.BaseServiceImpl;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * Created with generator tools.
 * User: sunguangzhu
 * DateTime:2014-02-19 05:00:53
 */
@Service
public class GroupServiceImpl extends BaseServiceImpl<Group> implements GroupService {
    @Resource
    private GroupDao groupDao;

    @Resource
    private UserDao userDao;

    @Override
    @Transactional
    public void updateceshi(Group target) {
        this.groupDao.save(target);
    }

    @Override
    @Transactional
    public void deleteMoreCascade(Long id) {
        List<Criterion> criterions = new ArrayList<Criterion>();
        criterions.add(Restrictions.eq("parent.id", id));
        List<Group> groups = this.groupDao.listByCriteria(criterions);
        this.deleteMoreCascade(groups);

        criterions.clear();
        criterions.add(Restrictions.eq("group.id", id));
        List<User> users = userDao.listByCriteria(criterions);
        for (User user : users) {
            this.userDao.delete(user);
        }
        this.groupDao.delete(id);
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Transactional
    private void deleteMoreCascade(List<Group> groups) {
        for (Group group : groups) {
            List<Criterion> criterions = new ArrayList<Criterion>();
            criterions.add(Restrictions.eq("parent.id", group.getId()));
            List<Group> groupList = this.groupDao.listByCriteria(criterions);
            if (groupList != null && groupList.size() > 0) {
                this.deleteMoreCascade(groupList);
            } else {
                criterions.clear();
                criterions.add(Restrictions.eq("group.id", group.getId()));
                List<User> users = userDao.listByCriteria(criterions);
                for (User user : users) {
                    this.userDao.delete(user);
                }
                this.groupDao.delete(group);
            }
        }
        //To change body of created methods use File | Settings | File Templates.
    }

    @Transactional
    @Override
    public void batchUpdateTest() {
        this.userDao.deleteAll();
        this.groupDao.deleteAll();
        long start = Calendar.getInstance().getTimeInMillis();
        for (int i = 0; i < 1000000; i++) {
            Group group = new Group();
            group.setName("group" + i);
            User user = new User();
            user.setName("user" + i);
            user.setGroup(group);
            this.groupDao.save(group);
            this.userDao.save(user);
        }
        long end = Calendar.getInstance().getTimeInMillis();
        System.out.println("end-start = " + (end - start));
        //To change body of implemented methods use File | Settings | File Templates.
    }

}

